package com.ice.app.system.user.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.util.StringUtil;
import com.ice.app.system.entity.User;
import com.ice.app.system.mapper.UserMapper;
import com.ice.app.system.utils.ConfigUtils;
import com.iceframework.core.entity.Result;
import com.iceframework.core.utils.KeyUtils;
import com.iceframework.core.utils.MD5Util;

/**
 * 
 * 业务逻辑层：
 * 系统用户
 * 
 * @author Mr.Palo
 * @email 442803840@qq.com
 * 
 */
@Service
public class UserService {

	@Autowired
	private UserMapper userMapper;
	
	// ----------------------------------------自动生成--------------------------------------------
	
	/**
	 * 菜单保存
	 * 
	 * @param user
	 * @return
	 * @throws Exception
	 */
	@Transactional
	public boolean saveUser(User user) throws Exception {
		if (StringUtil.isNotEmpty(user.getUserId())) {
			// 修改操作
			User old = this.getUser(user.getUserId());
			user.setUpdateBy("admin");
			user.setUpdateDate(new Date());
			if (old != null) {
				return userMapper.updateByPrimaryKeySelective(user) > 0;
			}
			return false;
		} else {
			// 保存操作
			String key = KeyUtils.genKey("User");
			user.setUserId(key);
			user.setCreateBy("admin");
			user.setCreateDate(new Date());
			user.setUpdateBy("admin");
			user.setUpdateDate(new Date());
			return userMapper.insertSelective(user) > 0;
		}
	}

	/**
	 * 根据主键删除菜单
	 * 
	 * @param key
	 * @return
	 */
	@Transactional
	public boolean deleteUser(String key) {
		return userMapper.deleteByPrimaryKey(key) > 0;
	}

	/**
	 * 根据多个主键删除菜单
	 * 
	 * @param keys
	 * @return
	 */
	@Transactional
	public boolean deleteUsers(String keys) {
		if (!StringUtils.isEmpty(keys)) {
			String[] strs = keys.split(",");
			for (String key : strs) {
				this.deleteUser(key);
			}
			return true;
		}
		return false;
	}

	/**
	 * 根据主键获取菜单实体
	 * 
	 * @param key
	 * @return
	 */
	public User getUser(String key) {
		return userMapper.selectByPrimaryKey(key);
	}
	
	/**
	 * 根据主键获取菜单实体
	 * 
	 * @param key
	 * @return
	 */
	public User getUserByUsername(String username) {
		return userMapper.selectByUsername(username);
	}

	/**
	 * 根据给定条件获取菜单实体集合
	 * 
	 * @param key
	 * @return
	 */
	public List<User> getUsers(User user) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user", user);
		return userMapper.selectByFilter(map);
	}

	// ----------------------------------------自定义（Public）---------------------------------------------
	@Transactional
	public boolean disableUser(String keys) {
		boolean result = true;
		if(!StringUtils.isEmpty(keys)){
			String[] strs=keys.split(",");
			for (String id : strs) {
				User user = this.getUser(id);
				user.setStatus(0);
				result = result& userMapper.updateByPrimaryKeySelective(user)>0;
			}
			return result;
		}
		return false;
	}
	
	@Transactional
	public boolean enableUser(String keys) {
		boolean result = true;
		if(!StringUtils.isEmpty(keys)){
			String[] strs=keys.split(",");
			for (String id : strs) {
				User user = this.getUser(id);
				user.setStatus(1);
				result = result& userMapper.updateByPrimaryKeySelective(user)>0;
			}
			return result;
		}
		return false;
	}
	
	/**
	 * 密码重置
	 * @param user
	 * @return
	 * @throws Exception 
	 */
	@Transactional
	public Result saveResetPwd(String id) throws Exception {
		String password = ConfigUtils.getCacheConfig("Init_Password");
		if(StringUtils.isBlank(password)) {
			password = "ice.qwertyuiop";
		}
		User user = new User();
		user.setUserId(id);
		user.setPassword(MD5Util.MD5(password));
		boolean bl = userMapper.updateByPrimaryKeySelective(user) > 0;
		if(bl) {
			return Result.success().add("resetPassword", password).setMsg("修改密码成功");
		}else {
			return Result.fail().setMsg("修改密码失败");
		}
	}
}